import { Injectable } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { environment } from '@env/environment';

@Injectable()
export class SkWebService {

  // public dataObj = {
  //   count: Number,
  //   rows: []
  // }


  constructor(
    private _http: _HttpClient
  ) {
    //
  }

  /**
   * 修改密码
   * @param body 参数
   */
  public async updatePassword(body) {
    const reqUrl = `${environment.SERVER_URL}/manage/account/updatePassword`;
    const ret: any = await this._http.post(reqUrl, body).toPromise();
    return ret;
  }

  /**
   * 上传文件
   * @param fileArr 文件对象集合
   */
  public uploadFile(fileArr: any[], fileGroup: string) {
    // 数组除空
    fileArr = fileArr.filter(value => value);
    if (fileArr.length === 0) {
      return;
    }
    const reqUrl = `${environment.SERVER_URL}/manage/file/upload`;

    const formData: FormData = new FormData();
    fileArr.forEach(file => {
      formData.append('files', file, file.name);
    });
    formData.append('fileGroup', fileGroup); // 指定文件所属的文件组
    return this._http.post(reqUrl, formData).toPromise();
  }

  /**
   * 获取eos账号列表
   * @param accountName 模糊查询账号名称
   * @param pageIndex 页码
   * @param pageSize 每页数据条数
   */
  public async fetchEosAccount({ accountName, pageIndex = 1, pageSize = 10 }) {
    const body: any = {
      offset: (pageIndex - 1) * pageSize,
      limit: pageSize
    };
    if (accountName) {
      body.accountName = accountName;
    }

    const reqUrl = `${environment.SERVER_URL}/manage/eosaccount/list`;
    const ret: any = await this._http.post(reqUrl, body).toPromise();
    return ret;
  }

  /**
   * 重新创建账号
   */
  public async recreateAccount(billNoList: any[]) {
    if (billNoList.length <= 0) {
      return;
    }
    const reqUrl = `${environment.SERVER_URL}/manage/eosaccount/recreate`;
    const body = {
      billNoList: billNoList
    };
    return await this._http.post(reqUrl, body).toPromise();
  }

  /**
   * 获取日志列表
   */
  public async fetchServiceLog({ logContent, pageIndex = 1, pageSize = 10 }) {
    const body: any = {
      offset: (pageIndex - 1) * pageSize,
      limit: pageSize
    };
    if (logContent) {
      body.logContent = logContent;
    }

    const reqUrl = `${environment.SERVER_URL}/manage/servicelog/list`;
    const ret: any = await this._http.post(reqUrl, body).toPromise();
    return ret;
  }

  /**
   * 查询app类型分类
   */
  public async distinctAppType() {
    const body: any = {
      field: 'appType'
    };

    const reqUrl = `${environment.SERVER_URL}/manage/thirdpartyapp/distinct`;
    const ret: any = await this._http.post(reqUrl, body).toPromise();
    return ret;
  }

  /**
   * 查询app列表
   * @param appName app名称
   * @param appType app类型
   * @param pageIndex 页码
   * @param pageSize 每页数据条数
   */
  public async fetchThirdPartyApp({ keyword, pageIndex = 1, pageSize = 10 }) {
    const body: any = {
      offset: (pageIndex - 1) * pageSize,
      limit: pageSize
    };
    if (keyword) {
      body.keyword = keyword;
    }

    const reqUrl = `${environment.SERVER_URL}/manage/thirdpartyapp/fetch`;
    const ret: any = await this._http.post(reqUrl, body).toPromise();
    return ret;
  }

  /**
   * 新增或更新app
   * @param appInfo app相关信息
   */
  public async upsertThirdPartyApp(operate: string, appInfo: any) {
    let reqUrl;
    if (operate === 'create') {
      reqUrl = `${environment.SERVER_URL}/manage/thirdpartyapp/create`;
    } else {
      reqUrl = `${environment.SERVER_URL}/manage/thirdpartyapp/update`;
    }
    const ret: any = await this._http.post(reqUrl, appInfo).toPromise();
    return ret;
  }

  /**
   * 删除app
   * @param appName app名称
   */
  public async deleteThirdPartyApp(appNameList: any[]) {
    appNameList = appNameList.filter(value => value);
    if (appNameList.length === 0) {
      return;
    }
    const body: any = {
      appNameList: appNameList
    };
    const reqUrl = `${environment.SERVER_URL}/manage/thirdpartyapp/delete`;
    const ret: any = await this._http.post(reqUrl, body).toPromise();
    return ret;
  }




  // 以下的api暂时无用
  // =======================================================================================
  /**
   * 获取节点列表
   * @param body 参数
   */
  public async fetchEndpoint(body) {
    const reqUrl = `${environment.SERVER_URL}/manage/eosendpoint/list`;
    const ret: any = await this._http.post(reqUrl, JSON.stringify(body)).toPromise();
    if (!ret.errcode && ret.data) {
      return ret.data;
    }
  }

  /**
   * 新增节点
   * @param body 参数
   */
  public async createEndpoint(body) {
    const reqUrl = `${environment.SERVER_URL}/manage/eosendpoint/create`;
    const ret: any = await this._http.post(reqUrl, JSON.stringify(body)).toPromise();
    if (!ret.errcode && ret.data) {
      return ret.data;
    }
  }

  /**
   * 更新节点
   * @param body 参数
   */
  public async updateEndpoint(body) {
    const reqUrl = `${environment.SERVER_URL}/manage/eosendpoint/update`;
    const ret: any = await this._http.post(reqUrl, JSON.stringify(body)).toPromise();
    if (!ret.errcode && ret.data) {
      return ret.data;
    }
  }

  /**
   * 查询支付类型列表
   * @param body 参数
   */
  public async fetchPayment() {
    const reqUrl = `${environment.SERVER_URL}/manage/payment/list`;
    const ret: any = await this._http.post(reqUrl).toPromise();
    if (!ret.errcode && ret.data) {
      return ret.data;
    }
  }

  /**
   * 创建支付类型
   * @param body 参数
   */
  public async createPayment(body) {
    const reqUrl = `${environment.SERVER_URL}/manage/payment/create`;
    const ret: any = await this._http.post(reqUrl, JSON.stringify(body)).toPromise();
    if (!ret.errcode && ret.data) {
      return ret.data;
    }
  }

  /**
   * 修改支付类型
   * @param body 参数
   */
  public async updatePayment(body) {
    const reqUrl = `${environment.SERVER_URL}/manage/payment/update`;
    const ret: any = await this._http.post(reqUrl, JSON.stringify(body)).toPromise();
    if (!ret.errcode && ret.data) {
      return ret.data;
    }
  }
}
